<template>
  <div style="padding:30px;" class="app-container">
    <el-form ref="Form" :inline="true" :model="Form" :rules="Rules">
      <el-select v-model="value" placeholder="请选择" style="width: 100px">
        <el-option
          label="姓名"
          value="1"
        />
        <el-option
          label="工号"
          value="2"
        />
      </el-select>
      <el-form-item v-if="vis1" prop="name">
        <el-input v-model="Form.name" placeholder="请输入用户姓名" :validate-event="false"/>
      </el-form-item>
      <el-form-item v-if="vis2" prop="uid">
        <el-input v-model="Form.uid" placeholder="请输入用户工号" :validate-event="false" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
      <el-button v-if="isShow1" type="primary" @click="getData">取消查询</el-button>
      <el-button type="primary" @click="handleExport">导出</el-button>
    </el-form>
    <el-dialog
      title="导出Excel"
      :visible.sync="ExportDialog"
      width="30%"
      :before-close="handleClose"
    >
      <div style="float: top">
        <div
          style="position:relative;top:10%;bottom:0;left: 0;
            margin: auto;right: 0;
            text-align:center"
        >
          <el-button type="primary" @click="handleExport1">导出所有数据</el-button>
          <el-button type="primary" @click="handleExport2">导出模板</el-button>
        </div>
        <div width="100%">
          <h3 style="text-align: center">你可以选择“导出模板”导出一张空白Excel</h3>
        </div>
      </div>
    </el-dialog>
    <el-table
      stripe
      border
      height="550"
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{'text-align': 'center'}"
    >
      <el-table-column
        prop="uname"
        label="姓名"
        width="150"
        align="center"
      />
      <el-table-column
        prop="jobNumber"
        label="工号"
        align="center"
      />
      <el-table-column
        prop="role"
        label="角色"
        align="center"
      />
      <el-table-column
        prop="maintenanceContents"
        label="工作内容"
        align="center"
      />
      <el-table-column
        prop="startProcessTime"
        label="工作开始时间"
        align="center"
      />
    </el-table>
    <el-pagination
      :current-page.sync="currentPage"
      :page-size="pageSize"
      layout="total, prev, pager, next, jumper"
      :total="total"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
<script>
import { getAllMaintainerInfo,getMaintainerInfo,getMaintainerInfo2 } from '@/api/maintainer'
import axios from 'axios'

export default {
  data() {
    const validateNum = (rule, value, callback) => {
      if (!(/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/).test(value)) {
        callback(new Error('请输入维修人员工号（数字值）'))
      } else {
        callback()
      }
    }
    return {
      total: 1,
      pageSize: 1,
      currentPage: 1,
      tableData: [],
      isShow1: false,
      value:"",
      vis1: true,
      vis2: false,
      Form: {
        name: '',
        uid: ''
      },
      Rules: {
        name: [
          { required: true, trigger: 'blur',message: '请输入维修人员姓名' }
        ],
        uid: [
          { required: true, trigger: 'blur', validator: validateNum }
        ]
      },
      ExportDialog: false,
    }
  },
  watch: {
    value: {
      deep: true,
      handler: function(newval, oldval) {
        if (newval === '1') {
          this.vis1 = true
          this.vis2 = false
        } else if (newval === '2') {
          this.vis2 = true
          this.vis1 = false
        }
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    getData() {
      getAllMaintainerInfo().then(({ data }) => {
        this.total = data.page.total
        this.pageSize = data.page.size
        this.tableData = data.page.records
        this.Form.name = ''
        this.Form.uid = ''
        this.isShow1 = false
      })
    },
    handleCurrentChange(pageNum) {
      getAllMaintainerInfo(pageNum).then(({ data }) => {
        this.total = data.page.total
        this.pageSize = data.page.size
        this.tableData = data.page.records
      })
    },
    handleClose(){
      this.ExportDialog = false
    },
    onSubmit() {
      this.$refs.Form.validate((valid) => {
        if (valid) {
          if (this.vis1 === true) {
            getMaintainerInfo(this.Form.name).then(({data}) => {
              if (data.page.total === 0) {
                this.$message({
                  showClose: true,
                  message: '没有查询到相应的维修人员信息',
                  type: 'error'
                })
              } else {
                this.total = data.page.total
                this.pageSize = data.page.size
                this.tableData = data.page.records
                this.Form.name = ''
                this.Form.uid = ''
                this.vis2 = false
                this.isShow1 = true
              }
            })
          } else {
            getMaintainerInfo2(this.Form.uid).then(({data}) => {
              if (data.page.total === 0) {
                this.$message({
                  showClose: true,
                  message: '没有查询到相应的维修人员信息',
                  type: 'error'
                })
              } else {
                this.total = data.page.total
                this.pageSize = data.page.size
                this.tableData = data.page.records
                this.Form.name = ''
                this.Form.uid = ''
                this.vis1 = false
                this.isShow1 = true
              }
            })
          }
        }
      })
    },
    handleExport() {
      this.ExportDialog = true
    },
    handleExport1() {
      axios({ // 用axios发送get请求
        method: 'get',
        url: process.env.VUE_APP_BASE_API + '/maintainer/export', // 请求地址
        responseType: 'blob', // 表明返回服务器返回的数据类型
        headers: {
          'Content-Type': 'application/json',
          'getResponse': 'true'
        }
      }).then(res => { // 处理返回的文件流
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })

        const disposition = res.headers['content-disposition']
        const filename = disposition.split(/filename=/i).pop()
        const fileName = decodeURI(filename)

        const elink = document.createElement('a')
        elink.download = fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL对象
        document.body.removeChild(elink)
      })
    },
    handleExport2() {
      axios({ // 用axios发送get请求
        method: 'get',
        url: process.env.VUE_APP_BASE_API + '/maintainer/extemplate', // 请求地址
        responseType: 'blob', // 表明返回服务器返回的数据类型
        headers: {
          'Content-Type': 'application/json',
          'getResponse': 'true'
        }
      }).then(res => { // 处理返回的文件流
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' })

        const disposition = res.headers['content-disposition']
        const filename = disposition.split(/filename=/i).pop()
        const fileName = decodeURI(filename)

        const elink = document.createElement('a')
        elink.download = fileName
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 释放URL对象
        document.body.removeChild(elink)
      })
    }
  }
}
</script>
